package com.minimvc.patterns.command
{
	import com.minimvc.interfaces.ICommand;
	import com.minimvc.interfaces.IReusable;
	import com.minimvc.patterns.observer.Notifier;
	
	import flash.events.Event;
	
	
	/**
	 * 对于一些原子处理命令的组合;
	 * @author crl
	 * 
	 */	
	public class MacroCommand extends Notifier implements ICommand ,IReusable
	{
		
		private var subCommands:Array;
		
		public function MacroCommand()
		{
			subCommands = new Array();
			initializeMacroCommand();			
		}
		
		protected function initializeMacroCommand():void
		{
		}

		protected function addSubCommand( commandClassRef:Class ): void
		{
			if(isReusable){
				subCommands.push(new commandClassRef());
				return;
			}
			subCommands.push(commandClassRef);
		}
		
		public function get isReusable():Boolean{
			return true;
		}
		
		public final function execute( event:Event ) : void
		{
			var commandInstance : ICommand
			while ( subCommands.length > 0) {
				if(isReusable){
					commandInstance=subCommands.shift();
				}else{
					var commandClassRef : Class = subCommands.shift();
					commandInstance = new commandClassRef();
				}
				commandInstance.execute( event );
			}
		}
								
	}
}